package com.css.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.css.service.IEmployeeService;
import com.css.vo.Employee;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class EmployeeAction extends ActionSupport {

	private Employee employee;
	private List<Employee> employeeList;
	private IEmployeeService employeeService;
	private Integer employeeId;

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public IEmployeeService getEmployeeService() {
		return employeeService;
	}

	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	public String execute() throws Exception {
		return SUCCESS;
	}

	public String addEmployee() {
		try {
			employeeService.save(employee);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String findAllEmployee() {
		try {
			employeeList = employeeService.findAllEmployee();
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String deleteEmployee() {
		try {
			HttpServletRequest request = (HttpServletRequest) ActionContext
					.getContext().get(ServletActionContext.HTTP_REQUEST);
			String employeeIdStr = request.getParameter("employeeId");
			Integer employeeId = Integer.parseInt(employeeIdStr);
			Employee employee = employeeService.findById(employeeId);
			employeeService.delete(employee);
			return SUCCESS;

		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String updateEmployeeView() {
		try {
			HttpServletRequest request = (HttpServletRequest) ActionContext
					.getContext().get(ServletActionContext.HTTP_REQUEST);
			String employeeIdStr = request.getParameter("employeeId");
			Integer employeeId = Integer.parseInt(employeeIdStr);
			System.out.println(employeeId);
			employee = employeeService.findById(employeeId);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String updateEmployee() {
		try {
			employeeService.update(employee);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
}
